namespace IAM.Interface.Common.Providers
{
    using IAM.Interface.Common.Encrypt;
    using System;
    using System.IO;
    using System.Net;
    using System.Text;

    public class HttpProvider : IProvider
    {
        public HttpProvider(string url, string xml)
        {
            base._url = url;
            base._xml = xml;
            base._endecrypt = new EncryptDecrypt();
        }

        public HttpProvider(string digest, string url, string xml)
        {
            base._sign = digest;
            base._url = url;
            base._xml = xml;
            base._endecrypt = new EncryptDecrypt();
        }

        public override string GetResponseStr()
        {
            string str = "";
            try
            {
                this.SetWholeSendStr();
                byte[] bs = Encoding.ASCII.GetBytes(base._sendstring);
                HttpWebRequest httpRequest = null;
                httpRequest = (HttpWebRequest) WebRequest.Create(base._url);
                httpRequest.Method = "POST";
                httpRequest.ContentType = "application/x-www-form-urlencoded";
                httpRequest.ContentLength = bs.Length;
                using (Stream reqStream = httpRequest.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);
                    reqStream.Flush();
                }
                HttpWebResponse httpResponse = (HttpWebResponse) httpRequest.GetResponse();
                if (httpRequest.HaveResponse)
                {
                    StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.Default);
                    str = sr.ReadToEnd();
                    sr.Close();
                    httpResponse.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return str;
        }
    }
}

